﻿namespace BM.Tools.WebControls.MForm.Controls.DefaultTemplates
{
    using System.Collections.ObjectModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using BM.Tools.WebControls.MForm.Controls.ValueHandlers;

    /// <summary>
    /// An <see cref="ITemplate" /> that creates a <see cref="DropDownListValueHandler" /> in the container control.
    /// </summary>
    public class ListBoxValueHandlerTemplate : ITemplate
    {
        private Collection<ListItem> options = new Collection<ListItem>();

        /// <summary>
        /// Gets Options.
        /// </summary>
        /// <value>
        /// The options.
        /// </value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Collection<ListItem> Options
        {
            get
            {                
                return options;
            }
        }

        /// <summary>
        /// Adds a provided list item to the items
        /// </summary>
        /// <param name="item">
        /// The list item to be added.
        /// </param>        
        public void AddListItem(ListItem item)
        {
            Options.Add(item);
        }

        /// <summary>
        /// Instantiates a <see cref="DropDownListValueHandler" /> in the container control.
        /// </summary>
        /// <param name="container">
        /// The container.
        /// </param>
        public void InstantiateIn(Control container)
        {
            var handler = new ListBoxValueHandler();
            handler.ListBox.Rows = 1;
            foreach (var option in Options)
            {
                handler.ListBox.Items.Add(new ListItem(option.Text, option.Value));
            }            

            container.Controls.Add(handler);
        }
    }
}